﻿using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;
using System.Windows.Forms;
namespace C4F.VistaP2P.Win.Chat
{
    /// <summary>
    /// This class enables WinForms Design Time support
    /// </summary>
    public class ChatDesignerActionList : DesignerActionList
    {
        public ChatDesignerActionList(IComponent component)
            : base(component)
        {
        }


        /// <summary>
        /// The list of items to show in the design tag
        /// </summary>
        /// <returns></returns>
        public override DesignerActionItemCollection GetSortedActionItems()
        {

            // Create list to store designer action items
            DesignerActionItemCollection actionItems = new DesignerActionItemCollection();


            //// Add Design category header text
            actionItems.Add(new DesignerActionHeaderItem("P2P Controls"));

            // Add Design category descriptive label
            actionItems.Add(
              new DesignerActionTextItem(
                "Properties that affect which control the Chat Control uses.",
                "Design"));

            // Dock/Undock designer action method item
            actionItems.Add(
              new DesignerActionMethodItem(
                this,
                "ToggleDockStyle",
                GetDockStyleText(),
                "Design",
                "Dock or undock this control in it's parent container.",
                true));


            actionItems.Add(
               new DesignerActionPropertyItem(
                 "Visible",
                 "Visible",
                 GetCategory(ChatControl, "Visible"),
                 GetDescription(ChatControl, "Visible")));

            actionItems.Add(
              new DesignerActionPropertyItem(
                "MessageLog",
                "Message Log",
                GetCategory(this.ChatControl, "MessageLog"),
                GetDescription(this.ChatControl, "MessageLog")));

            actionItems.Add(
             new DesignerActionPropertyItem(
               "NewMessage",
               "New Message",
               GetCategory(this.ChatControl, "NewMessage"),
               GetDescription(this.ChatControl, "NewMessage")));


            actionItems.Add(
             new DesignerActionPropertyItem(
               "SendMessage",
               "Send Message Button",
               GetCategory(this.ChatControl, "SendMessage"),
               GetDescription(this.ChatControl, "SendMessage")));

            // Return action list to design time item
            return actionItems;
        }


        /// <summary>
        /// The messagebox to hook up if they select it
        /// </summary>
        public TextBox NewMessage
        {
            get { return this.ChatControl.NewMessage; }
            set { SetProperty("NewMessage", value); }
        }

        /// <summary>
        /// The messagelog to hook up if they select it
        /// </summary>
        public TextBox MessageLog
        {
            get { return this.ChatControl.MessageLog; }
            set { SetProperty("MessageLog", value); }
        }

        /// <summary>
        /// The send button to hook up if they select it
        /// </summary>
        public Button SendMessage
        {
            get { return this.ChatControl.SendMessage; }
            set { SetProperty("SendMessage", value); }
        }


        /// <summary>
        /// Toggles the docking property on and off.
        /// </summary>
        public void ToggleDockStyle()
        {

            // Toggle ClockControl's Dock property
            if (this.ChatControl.Dock != DockStyle.Fill)
            {
                SetProperty("Dock", DockStyle.Fill);
            }
            else
            {
                SetProperty("Dock", DockStyle.None);
            }
        }
        
        /// <summary>
        /// Helper method that returns an appropriate
        /// display name for the Dock/Undock property,
        /// based on the property
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetDockStyleText()
        {
            if (this.ChatControl.Dock == DockStyle.Fill)
            {
                return "Undock in parent container";
            }
            else
            {
                return "Dock in parent container";
            }
        }

        /// <summary>
        /// Toggles if the control is visible or invisible
        /// </summary>
        public bool Visible
        {
            get { return this.ChatControl.ShadowVisible; }
            set { SetProperty("ShadowVisible", value); }
        }

        
        /// <summary>
        /// Helper property to acquire a chatControl reference
        /// </summary>
        private ChatControl ChatControl
        {
            get { return (ChatControl)this.Component; }
        }


        
        
        /// <summary>
        /// Helper method to safely set a component’s property 
        /// </summary>
        /// <param name="propertyName">the property name to set</param>
        /// <param name="value">the value</param>
        private void SetProperty(string propertyName, object value)
        {
            // Get property
            PropertyDescriptor property = TypeDescriptor.GetProperties(this.ChatControl)[propertyName];
            // Set property value
            property.SetValue(this.ChatControl, value);
        }

        /// <summary>
        /// Helper method to return the Category string from a
        /// CategoryAttribute assigned to a property exposed by 
        /// the specified object
        /// </summary>
        /// <param name="source">The control on the property to get</param>
        /// <param name="propertyName">the property name to get</param>
        /// <returns>the category of the property requested</returns>
        private static string GetCategory(object source, string propertyName)
        {
            PropertyInfo property = source.GetType().GetProperty(propertyName);
            CategoryAttribute attribute = (CategoryAttribute)property.GetCustomAttributes(typeof(CategoryAttribute), false)[0];
            if (attribute == null) return null;
            return attribute.Category;
        }

        
        /// <summary>
        /// Helper method to return the Description string from a
        /// DescriptionAttribute assigned to a property exposed by 
        /// the specified object 
        /// </summary>
        /// <param name="source">The control on the property to get the description</param>
        /// <param name="propertyName">the property name to get</param>
        /// <returns>the description string for this property</returns>
        private static string GetDescription(object source, string propertyName)
        {
            PropertyInfo property = source.GetType().GetProperty(propertyName);
            DescriptionAttribute attribute = (DescriptionAttribute)property.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
            if (attribute == null) return null;
            return attribute.Description;
        }
    }
}
